<?php
session_start();  

$con = mysql_connect("localhost","haschool_mdl1","B97C0HRYb7");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$db_selected = mysql_select_db("haschool_mdl1", $con);
if (!$db_selected) {
    die ('Can\'t use haschool_mdl1 : ' . mysql_error());
}  

$salt = '>;4khn&9g9?3d]{%xuVp@gir{vwp';
$password = md5($_POST[password].$salt);
$username = $_POST[username];	

$firstname = $_POST[firstname];
$lastname = $_POST[lastname];
$email = $_POST[email];
$phone1 = $_POST[phone1];
$phone2 = $_POST[phone2];
$city = $_POST[city];
$address = $_POST[address];

// Check if the parent account exist already
$sqlUsername= 'select * from mdl_user where username = \''.$username.'\'';
$result = mysql_query($sqlUsername);
$num_rows = mysql_num_rows($result);
if($num_rows>0){
	header( 'Location: http://haschoolmanagement.com/registration/new_parent_account.php?usernameexist='.$username) ;					  
} 
$sqlEmail= 'select * from mdl_user where email = \''.$email.'\'';
$resultEmail = mysql_query($sqlEmail);
$num_rows_email = mysql_num_rows($resultEmail);
if($num_rows_email>0){
	header( 'Location: http://haschoolmanagement.com/registration/new_parent_account.php?emailexist='.$email) ;					  
} 
if(($num_rows_email==null || $num_rows_email==0) && (($num_rows==null || $num_rows==0))){
	$sql='insert into mdl_user (username, password, firstname, lastname, email, phone1, phone2, city, address)
						values(\''.$username.'\', \''.$password.'\', \''.$firstname.'\', \''.
						$lastname.'\',\''.$email.'\', \''.$phone1.'\', \''.$phone2.
						'\', \''.$city.'\', \''.$address.'\')';

	mysql_query($sql);
	$userid = mysql_insert_id();
	
	// Create context for the new parent
	$sqlcontext = "insert into mdl_context (contextlevel, instanceid, path, depth)
					   values(30, $userid)";
	
	mysql_query($sqlcontext);
	$contextid = mysql_insert_id();
						
	mysql_close($con);
	if ($userid && $userid!=''){
		header( 'Location: http://haschoolmanagement.com/registration/subscription_ldc.php?username='.$username.'&password='.$password) ;			  
	}
}
mysql_close($con);

?>